package com.yunxun.coinmessage.service;

import com.yunxun.coinmessage.dao.UserDao;
import com.yunxun.coinmessage.entity.RecordVip;
import com.yunxun.coinmessage.entity.User;
import com.yunxun.coinmessage.templatedata.MsgResult;
import com.yunxun.coinmessage.sqlvo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    RecordVipService recordVipService;
    MsgResult msgResult;
    public List<User> queryAll(){
        return userDao.queryAll();
    }
    public List<User> queryByPage(UserVO userVO){
        List<User> oldUserList= userDao.queryByPage(userVO);
        List<User> newUserList=new ArrayList<>();
        for (User user:oldUserList){
            User u=userDao.queryRecom(user.getOpenid());
            if(u!=null){
                user.setUser_recom(u.getUname());
                user.setUserRecomId(u.getUid());
            }
            switch (user.getChannel()){
                case "ADD_SCENE_SEARCH":user.setChannel("公众号搜索"); break;
                case "ADD_SCENE_ACCOUNT_MIGRATION":user.setChannel("公众号迁移"); break;
                case "ADD_SCENE_PROFILE_CARD":user.setChannel("名片分享"); break;
                case "ADD_SCENE_QR_CODE":user.setChannel("扫描二维码"); break;
                case "ADD_SCENE_PROFILE_LINK":user.setChannel("图文页内名称点击"); break;
                case "ADD_SCENE_PROFILE_ITEM":user.setChannel("图文页右上角菜单"); break;
                case "ADD_SCENE_OTHERS":user.setChannel("其它"); break;
                default:user.setChannel("未知");
            }
            newUserList.add(user);
        }
        return newUserList;
    }
    public  Integer addUser(User user){
        return userDao.addUser(user);
    }
    public MsgResult changeUserStatus(User user){
        int tag;
        if(user.getStatus()==1){
            tag=userDao.changeStatus(user);
            if(tag>0){
                msgResult=new MsgResult(true,"启用成功");
                return msgResult;
            }else {
                msgResult=new MsgResult(false,"修改用户状态失败");
                return msgResult;
            }
        }else if(user.getStatus()==0){
            tag=userDao.changeStatus(user);
            if(tag>0){
                msgResult=new MsgResult(true,"禁用成功");
                return msgResult;
            }else {
                msgResult=new MsgResult(false,"修改用户状态失败");
                return msgResult;
            }
        }else{
            msgResult=new MsgResult(false,"请输入正确的状态码");
            return msgResult;
        }
    }
    public Integer queryTotal(){
        return  userDao.queryTotal();
    }
    public int queryTotalByName(String uname){
        return userDao.queryTotalByName(uname);
    }
    public User queryById(int uid){
        return userDao.queryById(uid);
    }
    public void updateUserLevel(User user){
        RecordVip recordVip=new RecordVip();
        long timeStamp=System.currentTimeMillis()/1000;
        recordVip.setCreate_time((int)timeStamp);
        recordVip.setUid(user.getUid());
        recordVipService.addRecordVip(recordVip);
        userDao.updateUserLevel(user);
    }
    public List<Integer> queryUidByUname(String uname){
        return userDao.queryUidByUname(uname);
    }
    public User queryUserByUid(int uid){
        return queryUserByUid(uid);
    }
}
